<?php
/* explain:
 * Project:     
 * File:        .php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . '../utils/CommonUtils.php');
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . '../domain/UserGroupDomain.php');

class UserGroupImpl extends CommonUtils
{
	
	function __construct($DBTag = 'system')
	{
		parent::__construct();
		
		$this->DBTag 	= $DBTag;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getById($id)
	{
		$sql = "SELECT name, levelId, status FROM user_groups WHERE id = $id";
		
		$obj = null;
		$conn = null;
		
		try {
			$conn = $this->getConnection($this->DBTag);
			
			$rs = mysql_query($sql, $conn);
			if ($row = mysql_fetch_row($rs)) {
				$obj = new UserGroupDomain();

				$obj->setId($id);
				$obj->setName($row[0]);				
				$obj->setLevelId($row[1]);
				$obj->setStatus($row[2]);
				
			} else {
				$this->message = "get data failed by Id. sql : $sql <br>";
				$this->message .= "error: " . mysql_error($conn);
			}
		} catch (Exception $e) {
			$this->message = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
			
		return $obj;

	}

	/* 
	 *
	 * @param  string  	name 	
	 * @param  string 	namecn
	 * @param  string 	status 		normal, stop
	 * @return boolean 	true or fail
	 * 
	 */
	public function add($name, $status = 'normal')
	{
		$sql = "INSERT INTO user_groups SET name = '$name', status='$status'";
		
		return $this->insert($this->DBTag, $sql);
	}
			
	/* edit
	 *
	 * @param  string 	id			modify id
	 * @param  string  	name 	
	 * @param  string 	namecn 
	 * @return boolean 	true or fail
	 */
	public function edit($id, $name)
	{
		$sql = "UPDATE user_groups SET name = '$name', WHERE id='$id'";
		
		return $this->update($this->DBTag, $sql);
	}
		
	/* delete
	 *
	 * @param  int    	id
	 * @param  return 	boolean
	 */
	public function delete($id)
	{
		$sql = "DELETE FROM user_groups WHERE id = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	public function lists($where, $order, $page, $discount, $url = '')
	{
		return $this->getPageRows($this->DBTag, '*', "user_groups $where", $order, $page, $discount, $url);	
	}
	
	public function getGroupSelect($groupid)
	{
		$sql = "SELECT id, name FROM user_groups WHERE id >= $groupid";
		
		return $this->getHash($this->DBTag, $sql);		
	}
}
?>
